<template>
  <div id="beijing" >
    <div id="login" style=" display: flex; height: 100vh; width: 100vw;  align-items: center; justify-content: center;" >
      <div id="login-box" style=" display: flex;  width: 60%;  background-color: white;  border-radius: 5px; overflow: hidden; height: 500px;">
        <div style="flex: 1">
          <img src="@/assets/login1.jpg" alt="" style="width: 100%;height: 60%;margin-top: 95px;margin-left: 20px;">
        </div>
        <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
          <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
            <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎注册实验室预约管理系统</div>
            <el-form-item prop="username">
              <el-input prefix-icon="User" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input prefix-icon="Lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item prop="confirmPass">
              <el-input prefix-icon="Lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
            </el-form-item>
            <el-form-item>
                <el-select v-model="user.role" placeholder="请选择角色" style="width: 100%">
                <el-option label="系统管理员" value="ADMIN"></el-option>
                <el-option label="老师" value="TEACHER"></el-option>
                <el-option label="学生" value="STUDENT"></el-option>
             </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="info" style="width: 100%" @click="register">注 册</el-button>
            </el-form-item>
            <div style="display: flex">
              <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer" @click="$router.push('/login')">登录</span></div>
            </div>
          </el-form>
        </div>
      </div>
  
    </div>
  </div>
    
  </template>
  
  <script setup>
  import ValidCode from "../components/ValidCode.vue";
  import { ref,reactive} from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus'
  import request from '../../utils/request';
  import { useRouter } from 'vue-router';
  import router from "../router"; 
  const registerRef = ref(null);
  const user = ref({
    username: '',
    password: '',
    confirmPass: ''
  });
  const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== user.value.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
  }
  const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  confirmPass: [{validator: validatePassword, trigger: 'blur'}],
  };
  const register = async () => {
  try {
    // 确保在调用 validate 时等待其完成
    await registerRef.value.validate(async (valid) => {
      if (valid) {
        // 验证通过，发起登录请求
        const res = await request.post('/register', user.value);
        console.log("11111111111111111111");
        console.log(user.value);
        if (res.data.code === '200') {
          router.push('/login');
          console.log(res);
          ElMessage.success('注册成功');
        } else {
          ElMessage.error(res.data.msg);
        }
      }
    });
  } catch (error) {
    console.error('登录请求失败:', error);
    ElMessage.error('登录请求失败，请稍后重试');
  }
};
  </script>
  
  <style scoped>
  #beijing{
    display: flex; 
    height: 100vh; 
    width: 100vw;  
    align-items: center; 
    justify-content: center;
  background-image: url('../assets/beijing.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  }
  </style>